package com.jysz.certificate.web.action.system;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.constant.OperCodeConstant;
import com.jysz.certificate.persistence.model.system.Menu;
import com.jysz.certificate.persistence.model.system.Role;
import com.jysz.certificate.persistence.model.system.RoleOperRef;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.system.RoleService;
import com.jysz.certificate.service.system.UserService;
import com.jysz.webcore.ssh.annotation.PermissionPoint;

/**
 * 权限管理
 * @author xuxiaolong
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/system/role")
public class RoleAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private static final Logger LOG = LoggerFactory.getLogger(RoleAction.class);

	@Resource
	private RoleService roleService;
	@Resource
	private UserService userService;

	private Role search;
	private Role entity;

	private Long id;
	private String userIds;
	private String userNames;
	private String codes;
	private String operNames;
	private List<Menu> opers;

	@Action(INDEX)
	public String index() {
		
		this.setPage(roleService.findPage(search, this.getCurrentPage(),
				this.getPageSize()));
		return INDEX;
	}

	@Action(ADD)
	public String add() {
		return ADD;
	}

	@Action(CREATE)
	public String create() {
		try {
			entity.setInitialize(false);
			roleService.save(entity, userIds, codes);
		} catch (Exception e) {
			setFailPopMessage("创建角色异常");
			LOG.error("创建角色异常", e);
		}

		setRedirectUrl("index.do");
		this.setSuccessPopMessage("角色创建成功");
		return SHOW_MSG_PAGE;
	}
	
	@Action("selectOper")
	public String selectOper() {
		
		opers=roleService.findOpers();
		return "selectOper";
	}
	
	@Action(EDIT)
	//@PermissionPoint(point=OperCodeConstant.SYS_ROLE_EDIT)
	public String edit() {
		entity = roleService.find(id);
		buildUserAndOperNames();
		return EDIT;
	}

	@Action(UPDATE)
	public String update() {

		try {
			Role role = roleService.find(entity.getId());
			role.setRoleName(entity.getRoleName());
			role.setMemo(entity.getMemo());

			roleService.save(role,userIds,codes);
			setSuccessPopMessage("更新数据成功");

		} catch (Exception e) {
			setFailPopMessage("更新数据异常");
			LOG.error("更新数据异常", e);
		}
		setRedirectUrl("index.do");
		return SHOW_MSG_PAGE;
	}

	@Action(DESTORY)
	@PermissionPoint(point=OperCodeConstant.SYS_ROLE_DEL,desc="角色删除")
	public String destory() {

		roleService.destory(id);
		setSuccessPopMessage("删除数据成功");
		setRedirectUrl("index.do?currentPage=" + getCurrentPage());
		return SHOW_MSG_PAGE;
	}

	private void buildUserAndOperNames(){
		if(entity!=null){
			if(entity.getUsers()!=null){
				StringBuilder sbId=new StringBuilder();
				StringBuilder sbName=new StringBuilder();
				for(User user:entity.getUsers()){
					sbId.append(user.getId()).append(",");
					sbName.append(user.getRealName()).append(",");
				}
				if(sbId.length()>1)
					sbId.deleteCharAt(sbId.length()-1);
				if(sbName.length()>1)
					sbName.deleteCharAt(sbName.length()-1);
				userIds=sbId.toString();
				userNames=sbName.toString();
			}
			if(entity.getRoleOperRefList()!=null){
				StringBuilder sbCode=new StringBuilder();
				StringBuilder sbName=new StringBuilder();
				for(RoleOperRef ror:entity.getRoleOperRefList()){
					sbCode.append(ror.getOper().getCode()).append(",");
					sbName.append(ror.getOper().getName()).append(",");
				}
				if(sbCode.length()>1)
					sbCode.deleteCharAt(sbCode.length()-1);
				if(sbName.length()>1)
					sbName.deleteCharAt(sbName.length()-1);
				codes=sbCode.toString();
				operNames=sbName.toString();
			}
		}
	}
	
	public Role getSearch() {
		return search;
	}

	public void setSearch(Role search) {
		this.search = search;
	}

	public Role getEntity() {
		return entity;
	}

	public void setEntity(Role entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public List<Menu> getOpers() {
		return opers;
	}

	public void setOpers(List<Menu> opers) {
		this.opers = opers;
	}

	public String getCodes() {
		return codes;
	}

	public void setCodes(String codes) {
		this.codes = codes;
	}

	public String getUserNames() {
		return userNames;
	}

	public void setUserNames(String userNames) {
		this.userNames = userNames;
	}

	public String getOperNames() {
		return operNames;
	}

	public void setOperNames(String operNames) {
		this.operNames = operNames;
	}

}
